function varargout = VideoProcess(varargin)
% VIDEOPROCESS M-file for VideoProcess.fig
%      VIDEOPROCESS, by itself, creates a new VIDEOPROCESS or raises the existing
%      singleton*.
%
%      H = VIDEOPROCESS returns the handle to a new VIDEOPROCESS or the handle to
%      the existing singleton*.
%
%      VIDEOPROCESS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in VIDEOPROCESS.M with the given input
%      arguments.
%
%      VIDEOPROCESS('Property','Value',...) creates a new VIDEOPROCESS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before VideoProcess_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to VideoProcess_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help VideoProcess

% Last Modified by GUIDE v2.5 28-Oct-2012 17:20:28

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @VideoProcess_OpeningFcn, ...
                   'gui_OutputFcn',  @VideoProcess_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT



% --- Executes just before VideoProcess is made visible.
function VideoProcess_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to VideoProcess (see VARARGIN)

% Choose default command line output for VideoProcess
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes VideoProcess wait for user response (see UIRESUME)
% uiwait(handles.figure1);


global inputfile;
global outputfile;

global inputfilefull;
global outputfilefull;

global frontviewfile;
global sideviewfile;

global frontviewfilefull;
global sideviewfilefull;

global finaloutputfile;
global finaloutputfilefull;

global backgroundfilefull;

global configfilefull;

global calibsubtractfilefront;
global calibsubtractfileside;

global calib2d3dfilefronthorz;
global calib2d3dfilesidehorz;
global calib2d3dfilefrontvert;

global calib2d3dfilefronthorzfull;
global calib2d3dfilesidehorzfull;
global calib2d3dfilefrontvertfull;


global calibsubtractfilefrontfull;
global calibsubtractfilesidefull;


global detectareasfile;
global detectareasfilefull;

global selecttypesfile;
global savefile;

global pathname;

global bFrontView;

selecttypesfile = 'selecttypes.mat';
savefile = 'videoprocessdata.mat';

if exist(savefile, 'file')
    load(savefile);
    
    if pathname == 0
        pathname = '';
    end
else
    inputfile = '';
    outputfile = '';

    inputfilefull = '';
    outputfilefull = '';

    frontviewfile = '';
    sideviewfile = '';

    frontviewfilefull = '';
    sideviewfilefull = '';

    finaloutputfile = '';
    finaloutputfilefull = '';

    backgroundfilefull = '';
    configfilefull = '';

    calibsubtractfilefront = '';
    calibsubtractfileside = '';               
    
    calibsubtractfilefrontfull = '';
    calibsubtractfilesidefull = '';
    
    calib2d3dfilefronthorz = '';
    calib2d3dfilesidehorz = '';
    calib2d3dfilefrontvert = '';
    
    calib2d3dfilefronthorzfull = '';
    calib2d3dfilesidehorzfull = '';
    calib2d3dfilefrontvertfull = '';
        
    detectareasfile = '';
    detectareasfilefull = '';
    
    pathname = '';
    
    bFrontView = -1;
end


UpdateEditBoxes(handles);
SaveData();




function SaveData()

global inputfile;
global outputfile;

global inputfilefull;
global outputfilefull;

global frontviewfile;
global sideviewfile;

global frontviewfilefull;
global sideviewfilefull;

global finaloutputfile;
global finaloutputfilefull;

global backgroundfilefull;
global configfilefull;

global calibsubtractfilefront;
global calibsubtractfileside;


global calib2d3dfilefronthorz;
global calib2d3dfilesidehorz;
global calib2d3dfilefrontvert;

global calib2d3dfilefronthorzfull;
global calib2d3dfilesidehorzfull;
global calib2d3dfilefrontvertfull;

global calibsubtractfilefrontfull;
global calibsubtractfilesidefull;

global detectareasfile;
global detectareasfilefull;

global bFrontView;

global savefile;
global selecttypesfile;

global pathname;

global nfrontstart;
global nsidestart;

global finaloutputfileconfig;

if ~isempty(inputfile)
    k = findstr(inputfile, 'front');
    
    if ~isempty(k)
        bFrontView = 1;
    else
        k = findstr(inputfile, 'side');
        
        if ~isempty(k)
            bFrontView = 0;
        end
    end
end

%if isempty(savefile)
%    savefile = 'videoprocessdata.mat';
%end

savefile = 'videoprocessdata.mat';

save(savefile, 'inputfile', 'outputfile', 'inputfilefull', 'outputfilefull', ...
    'frontviewfile', 'sideviewfile', 'frontviewfilefull', 'sideviewfilefull',...
    'finaloutputfile', 'finaloutputfilefull', 'backgroundfilefull', 'calibsubtractfilefront',... 
    'calibsubtractfileside', 'calibsubtractfilefrontfull', 'calibsubtractfilesidefull',... 
    'calib2d3dfilefronthorz', 'calib2d3dfilesidehorz', 'calib2d3dfilefrontvert',...
    'calib2d3dfilefronthorzfull', 'calib2d3dfilesidehorzfull', 'calib2d3dfilefrontvertfull',...
    'detectareasfile', 'detectareasfilefull', 'configfilefull', 'bFrontView',... 
    'selecttypesfile', 'pathname');

if ~isempty(finaloutputfileconfig)    
    save(finaloutputfileconfig, 'nfrontstart', 'nsidestart');
end


% --- Outputs from this function are returned to the command line.
function varargout = VideoProcess_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in buttoncalcbackground.
function buttoncalcbackground_Callback(hObject, eventdata, handles)
% hObject    handle to buttoncalcbackground (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global inputfile;
global outputfile;
global pathname;
global backgroundfilefull;


UpdateFileNames(handles);

if isempty(inputfile)
    errordlg('Please select an input file');
    return
elseif isempty(outputfile)     
    errordlg('Please select an output file');
    return
elseif isempty(backgroundfilefull)         
    len = length(inputfile);
    backgroundfilefull = strcat(inputfile(1:len-4), '_background.mat');
    backgroundfilefull = strcat(pathname, backgroundfilefull);
end


SaveData()

set(handles.textprocessvideo, 'String', 'Loading...')
iCreateBackground();
set(handles.textprocessvideo, 'String', '')






function UpdateFileNames(handles)

global inputfile;
global outputfile;

global inputfilefull;
global outputfilefull;

global frontviewfile;
global sideviewfile;

global frontviewfilefull;
global sideviewfilefull;

global finaloutputfile;
global finaloutputfilefull;

global calibsubtractfilefront;
global calibsubtractfileside;


global calib2d3dfilefronthorz;
global calib2d3dfilesidehorz;
global calib2d3dfilefrontvert;

global calib2d3dfilefronthorzfull;
global calib2d3dfilesidehorzfull;
global calib2d3dfilefrontvertfull;

global calibsubtractfilefrontfull;
global calibsubtractfilesidefull;

global detectareasfile;
global detectareasfilefull;

global backgroundfilefull;
global configfilefull;

global pathname;

global nfrontstart;
global nsidestart;


seditinputfile = get(handles.editinputfile, 'String');
seditoutputfile = get(handles.editoutputfile, 'String');

seditfrontviewfile = get(handles.editfrontview, 'String');
seditsideviewfile = get(handles.editsideview, 'String');
seditfinaloutputfile = get(handles.editfinaloutputfile, 'String');

seditcalibsubtractfilefront = get(handles.editsubtractfront, 'String');
seditcalibsubtractfileside = get(handles.editsubtractside, 'String');

seditcalib2d3dfilefronthorz = get(handles.edit2d3dcalibratefronthorz, 'String');
seditcalib2d3dfilesidehorz = get(handles.edit2d3dcalibratesidehorz, 'String');
seditcalib2d3dfilefrontvert = get(handles.edit2d3dcalibratefrontvert, 'String');

seditdetectareasfile = get(handles.editdetectareas, 'String');


nfrontstart = str2num(get(handles.editfrontstart, 'String'));
nsidestart = str2num(get(handles.editsidestart, 'String'));


%if isempty(inputfile) & ~isempty(seditinputfile)
    inputfile = seditinputfile;
    inputfilefull = seditinputfile;
%end



%if isempty(outputfile) & ~isempty(seditoutputfile)
    outputfile = seditoutputfile;
    outputfilefull = seditoutputfile;
%end

%if isempty(frontviewfile) & ~isempty(seditfrontviewfile)
    frontviewfile = seditfrontviewfile;
    frontviewfilefull = seditfrontviewfile;
%end

%if isempty(sideviewfile) & ~isempty(seditsideviewfile)
    sideviewfile = seditsideviewfile;
    sideviewfilefull = seditsideviewfile;
%end


%if isempty(finaloutputfile) & ~isempty(seditfinaloutputfile)
    finaloutputfile = seditfinaloutputfile;
    finaloutputfilefull = seditfinaloutputfile;
%end

%if isempty(calibsubtractfilefront) & ~isempty(seditcalibsubtractfilefront)
    calibsubtractfilefront = seditcalibsubtractfilefront;
    calibsubtractfilefrontfull = seditcalibsubtractfilefront;
%end

%if isempty(calibsubtractfileside) & ~isempty(seditcalibsubtractfileside)
    calibsubtractfileside = seditcalibsubtractfileside;
    calibsubtractfilesidefull = seditcalibsubtractfileside;
%end

%if isempty(calib2d3dfile) & ~isempty(seditcalib2d3dfile)
    calib2d3dfilefronthorz = seditcalib2d3dfilefronthorz;
    calib2d3dfilefronthorzfull = seditcalib2d3dfilefronthorz;
    
    calib2d3dfilesidehorz = seditcalib2d3dfilesidehorz;
    calib2d3dfilesidehorzfull = seditcalib2d3dfilesidehorz;
    
    calib2d3dfilefrontvert = seditcalib2d3dfilefrontvert;
    calib2d3dfilefrontvertfull = seditcalib2d3dfilefrontvert;
%end


%if isempty(detectareasfile) & ~isempty(seditdetectareasfile)
    detectareasfile = seditdetectareasfile;
    detectareasfilefull = seditdetectareasfile;
%end


%update filename for data containing calculated background
len = length(inputfile);

backgroundfilefull = strcat(inputfile(1:len-4), '_background.mat');
backgroundfilefull = strcat(pathname, backgroundfilefull);
        
%update filename for data containing calculated background
len = length(inputfile);
configfilefull = strcat(inputfile(1:len-4), '_config.mat');
configfilefull = strcat(pathname, configfilefull);

SaveData();


function Convert2D3DPushed(handles)

global calibsubtractfilefront;
global calibsubtractfileside;

global calib2d3dfilefronthorzfull;
global calib2d3dfilesidehorzfull;
global calib2d3dfilefrontvertfull;

global finaloutputfile;
global finaloutputfilefull;

global nfrontstart;
global nsidestart;

global finaloutputfileconfig;




set(handles.textconvert, 'String', 'Converting...');

if isempty(finaloutputfile) & isempty(finaloutputfilefull)
    errordlg('Please specify an output file');
    return
end

if isempty(finaloutputfilefull)
    finaloutputfilefull = finaloutputfile;
end

%calibsubtractfilefront
%calibsubtractfileside
if isempty(calibsubtractfilefront) | isempty(calibsubtractfileside)
    errordlg('Please select a both a frontview and sideview file for conversion');
    return
end

if isempty(calib2d3dfilefronthorzfull) | isempty(calib2d3dfilesidehorzfull) | isempty(calib2d3dfilefrontvertfull)
    errordlg('One or more 2d3d calibrations not specified. Please select all calibration files before 2d3d conversion.');
    return
end



if isempty(finaloutputfileconfig)
    len = length(finaloutputfile);
    finaloutputfileconfig = finaloutputfile(1:len-4);
    finaloutputfileconfig = strcat(finaloutputfileconfig, '_config.mat');        
end





scheck1 = dir(calibsubtractfilefront);
scheck2 = dir(calibsubtractfileside);
scheck3 = dir(calib2d3dfilefronthorzfull);
scheck4 = dir(calib2d3dfilesidehorzfull);
scheck5 = dir(calib2d3dfilefrontvertfull);


if isempty(scheck3)
    s = strcat('File not found: ', calib2d3dfilefronthorzfull);
    errordlg(s)
    return
end

if isempty(scheck4)
    s = strcat('File not found: ', calib2d3dfilesidehorzfull);
    errordlg(s)
    return
end

if isempty(scheck5)
    s = strcat('File not found: ', calib2d3dfilefrontvertfull);
    errordlg(s)
    return
end

nfrontstart = str2num(get(handles.editfrontstart, 'String'));
nsidestart = str2num(get(handles.editsidestart, 'String'));


save(finaloutputfileconfig, 'nfrontstart', 'nsidestart');

Convert2D3D(nfrontstart, nsidestart)
set(handles.textconvert, 'String', '');

function editsubtractfront_Callback(hObject, eventdata, handles)
% hObject    handle to editsubtractfront (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editsubtractfront as text
%        str2double(get(hObject,'String')) returns contents of editsubtractfront as a double


% --- Executes during object creation, after setting all properties.
function editsubtractfront_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editsubtractfront (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit2d3dcalibratefronthorz_Callback(hObject, eventdata, handles)
% hObject    handle to edit2d3dcalibratefronthorz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2d3dcalibratefronthorz as text
%        str2double(get(hObject,'String')) returns contents of edit2d3dcalibratefronthorz as a double


% --- Executes during object creation, after setting all properties.
function edit2d3dcalibratefronthorz_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2d3dcalibratefronthorz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in buttonimagesubtraction.
function buttonimagesubtraction_Callback(hObject, eventdata, handles)
% hObject    handle to buttonimagesubtraction (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

UpdateFileNames(handles);
iCalibrateSubtract();


% --- Executes on button press in pushbutton2d3dcalibratehorz.
function pushbutton2d3dcalibratehorz_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2d3dcalibratehorz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

UpdateFileNames(handles);
iCalibrateHorz();


function editinputfile_Callback(hObject, eventdata, handles)
% hObject    handle to editinputfile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editinputfile as text
%        str2double(get(hObject,'String')) returns contents of editinputfile as a double


% --- Executes during object creation, after setting all properties.
function editinputfile_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editinputfile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in buttonopenvideo.
function buttonopenvideo_Callback(hObject, eventdata, handles)
% hObject    handle to buttonopenvideo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global inputfile;
global outputfile;

global inputfilefull;
global outputfilefull;

global frontviewfile;
global sideviewfile;

global frontviewfilefull;
global sideviewfilefull;

global finaloutputfile;
global finaloutputfilefull;

global backgroundfilefull;
global configfilefull;

global bFrontView;

[filename, pathname] = uigetfile( ...
{'*.avi; *.mpg; *.mpeg','Video Files (*.avi, *.mpg, *.mpeg)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');

if filename ~= 0
    inputfile = filename;
    inputfilefull = strcat(pathname, filename);    
    
    a = strfind(filename, '.');
    
    if ~isempty(a)        
        
        %update output file name
        outputfile = strcat(filename(1:a-1), '.mat');
        outputfilefull = strcat(pathname, outputfile);
                
        %update filename for data containing calculated background
        len = length(inputfile);
        backgroundfilefull = strcat(inputfile(1:len-4), '_background.mat');
        backgroundfilefull = strcat(pathname, backgroundfilefull);
        
        %update filename for data containing calculated background
        len = length(inputfile);
        configfilefull = strcat(inputfile(1:len-4), '_config.mat');
        configfilefull = strcat(pathname, configfilefull);
                                              
        %set current input file as front or side view
        a = strfind(lower(filename), 'front');
        b = strfind(lower(filename), 'side');        
        
        if ~isempty(a) %video file name contains the word 'front' so update view to front
            
            %update front view file name
            frontviewfile = outputfile; 
            frontviewfilefull = outputfilefull; 
            
            bFrontView = 1;
           
                        
            [finaloutputfile, finaloutputfilefull] = GetFinalOutputFilename(a, filename, pathname);                              
                            
        elseif ~isempty(b) %video file name contains the word 'side' so update view to side
            
            %update front view file name
            sideviewfile = outputfile;            
            sideviewfilefull = outputfilefull;    
            
            bFrontView = 0;
       
        end        
        
    end
    
    UpdateEditBoxes(handles);    
    SaveData();
end

                
                
                
function editoutputfile_Callback(hObject, eventdata, handles)
% hObject    handle to editoutputfile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editoutputfile as text
%        str2double(get(hObject,'String')) returns contents of editoutputfile as a double


% --- Executes during object creation, after setting all properties.
function editoutputfile_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editoutputfile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in buttonoutputfile.
function buttonoutputfile_Callback(hObject, eventdata, handles)
% hObject    handle to buttonoutputfile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global outputfile;
global outputfilefull;

[filename, pathname] = uigetfile( ...
{'*.mat','Matlab Data Files (*.mat)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');

if filename ~= 0
    outputfile = filename;
    outputfilefull = strcat(pathname, filename);
    
    UpdateEditBoxes(handles);
    
    SaveData();
end



% --- Executes on button press in pushbuttonfrontview.
function pushbuttonfrontview_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonfrontview (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global frontviewfile;
global frontviewfilefull;

global finaloutputfile;
global finaloutputfilefull;


[filename, pathname] = uigetfile( ...
{'*.mat','Matlab Data Files (*.mat)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');

if filename ~= 0
    frontviewfile = filename;
    frontviewfilefull = strcat(pathname, filename);
        
    a = strfind(lower(filename), 'front');
                    
    if ~isempty(a)                                     
         [finaloutputfile, finaloutputfilefull] = GetFinalOutputFilename(a, filename, pathname);
    end
                
    UpdateEditBoxes(handles);
    SaveData();
end



% --- Executes on button press in pushbuttonsideview.
function pushbuttonsideview_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonsideview (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


global sideviewfile;
global sideviewfilefull;

[filename, pathname] = uigetfile( ...
{'*.mat','Matlab Data Files (*.mat)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');


if filename ~= 0
    sideviewfile = filename;
    sideviewfilefull = strcat(pathname, filename);
    
    UpdateEditBoxes(handles);
    
    SaveData();
end





function editfrontview_Callback(hObject, eventdata, handles)
% hObject    handle to editfrontview (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editfrontview as text
%        str2double(get(hObject,'String')) returns contents of editfrontview as a double


% --- Executes during object creation, after setting all properties.
function editfrontview_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfrontview (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editsideview_Callback(hObject, eventdata, handles)
% hObject    handle to editsideview (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editsideview as text
%        str2double(get(hObject,'String')) returns contents of editsideview as a double


% --- Executes during object creation, after setting all properties.
function editsideview_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editsideview (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttonsubtractfront.
function pushbuttonsubtractfront_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonsubtractfront (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global calibsubtractfilefront;
global calibsubtractfilefrontfull;


[filename, pathname] = uigetfile( ...
{'*.mat','Matlab Data Files (*.mat)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');


if filename ~= 0
    calibsubtractfilefront = filename;
    calibsubtractfilefrontfull = strcat(pathname, filename);
    
    UpdateEditBoxes(handles);
    
    SaveData();
end



% --- Executes on button press in pushbutton2d3dfronthorz.
function pushbutton2d3dfronthorz_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2d3dfronthorz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global calib2d3dfilefronthorz;
global calib2d3dfilefronthorzfull;

[filename, pathname] = uigetfile( ...
{'*.mat','Matlab Data Files (*.mat)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');


if filename ~= 0
    calib2d3dfilefronthorz = filename;
    calib2d3dfilefronthorzfull = strcat(pathname, filename);
    
    UpdateEditBoxes(handles);
    
    SaveData();
end



% --- Executes on selection change in listboxinputfiles.
function listboxinputfiles_Callback(hObject, eventdata, handles)
% hObject    handle to listboxinputfiles (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listboxinputfiles contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxinputfiles


% --- Executes during object creation, after setting all properties.
function listboxinputfiles_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listboxinputfiles (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in buttonaddvptobatch.
function buttonaddvptobatch_Callback(hObject, eventdata, handles)
% hObject    handle to buttonaddvptobatch (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in buttonadd2d3dtobatch.
function buttonadd2d3dtobatch_Callback(hObject, eventdata, handles)
% hObject    handle to buttonadd2d3dtobatch (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on selection change in listboxbatchvideos.
function listboxbatchvideos_Callback(hObject, eventdata, handles)
% hObject    handle to listboxbatchvideos (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listboxbatchvideos contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxbatchvideos


% --- Executes during object creation, after setting all properties.
function listboxbatchvideos_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listboxbatchvideos (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in buttonconvert2d3d.
function buttonconvert2d3d_Callback(hObject, eventdata, handles)
% hObject    handle to buttonconvert2d3d (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

UpdateFileNames(handles);
Convert2D3DPushed(handles);


function editfinaloutputfile_Callback(hObject, eventdata, handles)
% hObject    handle to editfinaloutputfile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editfinaloutputfile as text
%        str2double(get(hObject,'String')) returns contents of editfinaloutputfile as a double


% --- Executes during object creation, after setting all properties.
function editfinaloutputfile_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfinaloutputfile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttonoutputfile2d3d.
function pushbuttonoutputfile2d3d_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonoutputfile2d3d (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global finaloutputfile;
global finaloutputfilefull;


[filename, pathname] = uigetfile( ...
{'*.mat','Matlab Data Files (*.mat)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');

if filename ~= 0
    finaloutputfile = filename;
    finaloutputfilefull = strcat(pathname, filename);            
                
    UpdateEditBoxes(handles);
    SaveData();
end



% --- Executes on selection change in listboxbatch2d3d.
function listboxbatch2d3d_Callback(hObject, eventdata, handles)
% hObject    handle to listboxbatch2d3d (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listboxbatch2d3d contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxbatch2d3d


% --- Executes during object creation, after setting all properties.
function listboxbatch2d3d_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listboxbatch2d3d (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in buttonremovebatchvideo.
function buttonremovebatchvideo_Callback(hObject, eventdata, handles)
% hObject    handle to buttonremovebatchvideo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in buttonremovebatch2d3d.
function buttonremovebatch2d3d_Callback(hObject, eventdata, handles)
% hObject    handle to buttonremovebatch2d3d (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in buttonrunbatch.
function buttonrunbatch_Callback(hObject, eventdata, handles)
% hObject    handle to buttonrunbatch (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in buttonprocessvideo.
function buttonprocessvideo_Callback(hObject, eventdata, handles)
% hObject    handle to buttonprocessvideo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global inputfilefull;

UpdateFileNames(handles);

if isempty(inputfilefull)
    errordlg('Please select an input file');
    return
end

SaveData();

set(handles.textprocessvideo, 'String', 'Loading...')
iProcessVideo();
set(handles.textprocessvideo, 'String', '')


% --- Executes on button press in pushbuttonremove.
function pushbuttonremove_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonremove (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)







function [finaloutputfile, finaloutputfilefull] = GetFinalOutputFilename(a, filename, pathname)
                
                
len = length(filename);
a2 = a+4;
                
if a2 < len
    filename(a:a2) = [];
                    
    a = strfind(filename, '.');
                    
    finaloutputfile = strcat('final', filename(1:a-1));
    finaloutputfile = strcat(finaloutputfile, '.mat');
                    
    finaloutputfilefull = strcat(pathname,finaloutputfile);                                                           
end



function UpdateEditBoxes(handles)

global inputfile;
global outputfile;
global frontviewfile;
global sideviewfile;
global finaloutputfile;

global calibsubtractfilefront;
global calibsubtractfileside;

global calib2d3dfilefronthorz;
global calib2d3dfilesidehorz;
global calib2d3dfilefrontvert;

global detectareasfile;

global finaloutputfileconfig;

global nfrontstart;
global nsidestart;

set(handles.editinputfile, 'String', inputfile);
set(handles.editoutputfile, 'String', outputfile);

set(handles.editfrontview, 'String', frontviewfile);
set(handles.editsideview, 'String', sideviewfile);       

set(handles.editfinaloutputfile, 'String', finaloutputfile);


set(handles.editsubtractfront, 'String', calibsubtractfilefront);
set(handles.editsubtractside, 'String', calibsubtractfileside);
set(handles.edit2d3dcalibratefronthorz, 'String', calib2d3dfilefronthorz);
set(handles.edit2d3dcalibratesidehorz, 'String', calib2d3dfilesidehorz);
set(handles.edit2d3dcalibratefrontvert, 'String', calib2d3dfilefrontvert);

set(handles.editdetectareas, 'String', detectareasfile);


if ~isempty(finaloutputfile)
    len = length(finaloutputfile);
    finaloutputfileconfig = finaloutputfile(1:len-4);
    finaloutputfileconfig = strcat(finaloutputfileconfig, '_config.mat');
    
    scheck = dir(finaloutputfileconfig);
    
    if ~isempty(scheck)
        load(finaloutputfileconfig);
        
        set(handles.editfrontstart, 'String', num2str(nfrontstart));
        set(handles.editsidestart, 'String', num2str(nsidestart));
    end
end


function editsubtractside_Callback(hObject, eventdata, handles)
% hObject    handle to editsubtractside (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editsubtractside as text
%        str2double(get(hObject,'String')) returns contents of editsubtractside as a double


% --- Executes during object creation, after setting all properties.
function editsubtractside_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editsubtractside (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttonsubtractside.
function pushbuttonsubtractside_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonsubtractside (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global calibsubtractfileside;
global calibsubtractfilesidefull;


[filename, pathname] = uigetfile( ...
{'*.mat','Matlab Data Files (*.mat)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');


if filename ~= 0
    calibsubtractfileside = filename;
    calibsubtractfilesidefull = strcat(pathname, filename);
    
    UpdateEditBoxes(handles);
    
    SaveData();
end



function editdetectareas_Callback(hObject, eventdata, handles)
% hObject    handle to editdetectareas (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editdetectareas as text
%        str2double(get(hObject,'String')) returns contents of editdetectareas as a double


% --- Executes during object creation, after setting all properties.
function editdetectareas_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editdetectareas (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttondetectareas.
function pushbuttondetectareas_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttondetectareas (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


global detectareasfile;
global detectareasfilefull;

[filename, pathname] = uigetfile( ...
{'*.mat','Matlab Data Files (*.mat)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');


if filename ~= 0
    detectareasfile = filename;
    detectareasfilefull = strcat(pathname, filename);
    
    UpdateEditBoxes(handles);
    
    SaveData();
end




% --- Executes on button press in pushbuttondetectareasconfigure.
function pushbuttondetectareasconfigure_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttondetectareasconfigure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

UpdateFileNames(handles);
iSelectAreas();


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

UpdateFileNames(handles);

SaveData();

% Hint: delete(hObject) closes the figure
delete(hObject);


% --- Executes on button press in pushbuttonresetfilenames.
function pushbuttonresetfilenames_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonresetfilenames (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global inputfile;
global outputfile;

global inputfilefull;
global outputfilefull;

global frontviewfile;
global sideviewfile;

global frontviewfilefull;
global sideviewfilefull;

global finaloutputfile;
global finaloutputfilefull;

global backgroundfilefull;

global configfilefull;

global calibsubtractfilefront;
global calibsubtractfileside;
global calibsubtractfilefrontfull;
global calibsubtractfilesidefull;


global calib2d3dfilefronthorz;
global calib2d3dfilesidehorz;
global calib2d3dfilefrontvert;
global calib2d3dfilefronthorzfull;
global calib2d3dfilesidehorzfull;
global calib2d3dfilefrontvertfull;


global detectareasfile;
global detectareasfilefull;


inputfile = '';
outputfile = '';

inputfilefull = '';
outputfilefull = '';

frontviewfile = '';
sideviewfile = '';

frontviewfilefull = '';
sideviewfilefull = '';

finaloutputfile = '';
finaloutputfilefull = '';

backgroundfilefull = '';
configfilefull = '';

calibsubtractfilefront = '';
calibsubtractfileside = '';
calib2d3dfilefronthorz = '';
calib2d3dfilesidehorz = '';
calib2d3dfilefrontvert = '';
    
calibsubtractfilefrontfull = '';
calibsubtractfilesidefull = '';
calib2d3dfilefronthorzfull = '';
calib2d3dfilesidehorzfull = '';
calib2d3dfilefrontvertfull = '';
    
detectareasfile = '';
detectareasfilefull = '';

UpdateEditBoxes(handles);
SaveData();



function edit2d3dcalibratesidehorz_Callback(hObject, eventdata, handles)
% hObject    handle to edit2d3dcalibratesidehorz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2d3dcalibratesidehorz as text
%        str2double(get(hObject,'String')) returns contents of edit2d3dcalibratesidehorz as a double


% --- Executes during object creation, after setting all properties.
function edit2d3dcalibratesidehorz_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2d3dcalibratesidehorz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton2d3dsidehorz.
function pushbutton2d3dsidehorz_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2d3dsidehorz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


global calib2d3dfilesidehorz;
global calib2d3dfilesidehorzfull;

[filename, pathname] = uigetfile( ...
{'*.mat','Matlab Data Files (*.mat)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');


if filename ~= 0
    calib2d3dfilesidehorz = filename;
    calib2d3dfilesidehorzfull = strcat(pathname, filename);
    
    UpdateEditBoxes(handles);
    
    SaveData();
end




function edit2d3dcalibratefrontvert_Callback(hObject, eventdata, handles)
% hObject    handle to edit2d3dcalibratefrontvert (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2d3dcalibratefrontvert as text
%        str2double(get(hObject,'String')) returns contents of edit2d3dcalibratefrontvert as a double


% --- Executes during object creation, after setting all properties.
function edit2d3dcalibratefrontvert_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2d3dcalibratefrontvert (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes on button press in pushbutton2d3dfrontvert.
function pushbutton2d3dfrontvert_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2d3dfrontvert (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global calib2d3dfilefrontvert;
global calib2d3dfilefrontvertfull;

[filename, pathname] = uigetfile( ...
{'*.mat','Matlab Data Files (*.mat)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');

if filename ~= 0
    calib2d3dfilefrontvert = filename;
    calib2d3dfilefrontvertfull = strcat(pathname, filename);
    
    UpdateEditBoxes(handles);
    
    SaveData();
end





% --- Executes on button press in pushbutton2d3dcalibratevert.
function pushbutton2d3dcalibratevert_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2d3dcalibratevert (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

UpdateFileNames(handles);
iCalibrateVert();



function editfrontstart_Callback(hObject, eventdata, handles)
% hObject    handle to editfrontstart (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editfrontstart as text
%        str2double(get(hObject,'String')) returns contents of editfrontstart as a double


% --- Executes during object creation, after setting all properties.
function editfrontstart_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfrontstart (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editsidestart_Callback(hObject, eventdata, handles)
% hObject    handle to editsidestart (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editsidestart as text
%        str2double(get(hObject,'String')) returns contents of editsidestart as a double


% --- Executes during object creation, after setting all properties.
function editsidestart_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editsidestart (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in resetinputoutputfiles.
function resetinputoutputfiles_Callback(hObject, eventdata, handles)
% hObject    handle to resetinputoutputfiles (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




global inputfile;
global outputfile;

global inputfilefull;
global outputfilefull;

global frontviewfile;
global sideviewfile;

global frontviewfilefull;
global sideviewfilefull;

global finaloutputfile;
global finaloutputfilefull;

global backgroundfilefull;

global configfilefull;


inputfile = '';
outputfile = '';

inputfilefull = '';
outputfilefull = '';

frontviewfile = '';
sideviewfile = '';

frontviewfilefull = '';
sideviewfilefull = '';

finaloutputfile = '';
finaloutputfilefull = '';

backgroundfilefull = '';
configfilefull = '';


UpdateEditBoxes(handles);
SaveData();
